//
//  WBHomeTableViewController.swift
//  weibo
//
//  Created by 胡古古 on 2017/6/4.
//  Copyright © 2017年 hugugu. All rights reserved.
//

import UIKit

class WBHomeTableViewController: WBVisitorTableViewController {
    
    fileprivate lazy var homeViewModel:WBHomeViewModel = WBHomeViewModel()
    
    // 上拉加载控件
    fileprivate lazy var pullupView: UIActivityIndicatorView = {
        
       let indicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge)
        
        indicatorView.color = UIColor.red
        
        return indicatorView
    }()
    
    // tip消息
    fileprivate lazy var tipLabel: UILabel = {
        
        let label = UILabel(fontSize: 12, textColor: UIColor.white)
        label.backgroundColor = UIColor.orange
        label.text = "没有价值到最新微博数据"
        label.textAlignment = .center
        label.isHidden = true
        
        return label
    }()
    
    fileprivate lazy var pullDownView: WBRefreshControl = {
        
        let ctr = WBRefreshControl()
        
        ctr.addTarget(self, action: #selector(pullDownRefreshing), for: .valueChanged)
        
        return ctr
        
    }()


    override func viewDidLoad() {
        super.viewDidLoad()
        
        if !isLogin {
            
            visitorView?.upDateVisitorViewInfo(imageName: nil, message: nil)
            
        } else {
            // 登录成功
            setupUI()
            loadData()
            
        }

    }
    
    private func setupUI() {
        
        if let nav = self.navigationController {
            
            nav.view.insertSubview(tipLabel, belowSubview: nav.navigationBar)
            tipLabel.frame = CGRect(x: 0, y: nav.navigationBar.frame.maxY - 35, width:nav.navigationBar.bounds.width, height: 35)
        }
        setTableView()
        
        
    }
    
    
    private func setTableView() {

        tableView.estimatedRowHeight = 200
        
        tableView.separatorStyle = .none
        
        tableView.register(WBHomeTableViewCell.self, forCellReuseIdentifier: HomeCell_ID)
        
        // 添加底部上拉属性控件
        tableView.tableFooterView = pullupView
        // 下拉刷新 
        tableView.addSubview(pullDownView)
        
    }
    
    @objc private func pullDownRefreshing() {
        
        print("下拉刷新")
        loadData()
    }
    
    fileprivate func loadData() {
        
        homeViewModel.loadData(isUp: pullupView.isAnimating) { (isSuccess:Bool,message:String) in
            
            self.endRefreshing()
            
            if self.pullupView.isAnimating == false {
                
                self.startTipAnimation(message: message)
            }
            
            if isSuccess {
                
                self.tableView.reloadData()
            }
            
        }
        
    }
    // 结束刷新
    fileprivate func endRefreshing() {
        
        pullupView.stopAnimating()
        pullDownView.endRefreshing()
        
    }
    
    // MARK: --tip动画
    fileprivate func startTipAnimation(message:String) {
        
        if tipLabel.isHidden == false {
            
            return
        }
        
        // 显示tip
        tipLabel.isHidden = false
        tipLabel.text = message

        UIView.animate(withDuration: 1, animations: { 
            
            self.tipLabel.transform = CGAffineTransform(translationX: 0, y:self.tipLabel.bounds.size.height)
        }) { (_) in
            UIView.animate(withDuration: 1, animations: { 
                
                self.tipLabel.transform = CGAffineTransform.identity
            }, completion: { (_) in
                
                self.tipLabel.isHidden = true
            })
        }
    }
}

extension WBHomeTableViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return homeViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: HomeCell_ID, for: indexPath) as! WBHomeTableViewCell
        
        cell.statusViewModel = homeViewModel.statusList[indexPath.row]
        
        cell.selectionStyle = .none
        return cell
        
    }
    
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        if indexPath.row == homeViewModel.statusList.count - 1 && !pullupView.isAnimating {
            
            print("滚动到最后一个cell了")
            pullupView.startAnimating()
            loadData()
            
        }
        
    }
    
}
